package com.peng.leetcode.tree;

import java.util.ArrayDeque;
import java.util.Queue;

/**
 * MirrorTree1
 * <p>
 * Created on 2022/11/20
 *
 * @author lupeng
 */
public class MirrorTree1 {

    public TreeNode mirrorTree(TreeNode root) {
        if (root == null) {
            return null;
        }
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        mirrorTree(root.left);
        mirrorTree(root.right);
        return root;
    }

    public TreeNode mirrorTree1(TreeNode root) {
        if (root == null) {
            return null;
        }
        Queue<TreeNode> q = new ArrayDeque<>();
        q.offer(root);
        while (!q.isEmpty()) {
            TreeNode n = q.poll();
            if (n.left != null) {
                q.offer(n.left);
            }
            if (n.right != null) {
                q.offer(n.right);
            }
            TreeNode temp = n.left;
            n.left = n.right;
            n.right = temp;
        }
        return root;
    }
}
